package com.duoketech.service.weixin.memberinfo;

import com.duoketech.exception.ValidateException;
import com.duoketech.model.member.Member;
import com.duoketech.model.memberinfo.MemberInfo;
import com.duoketech.service.web.member.MemberService;
import com.duoketech.utils.validator.Validator;
import com.duoketech.utils.validator.rules.string.StringLengthBetween;
import com.duoketech.utils.validator.rules.string.StringMustBeEmail;
import com.duoketech.utils.validator.rules.string.StringMustBePassWord;
import com.duoketech.utils.validator.rules.string.StringMustBeRequired;
import com.jfinal.kit.HashKit;

import java.util.Date;

public class WxMemberInfoService {

    public static WxMemberInfoService ME = new WxMemberInfoService();

    public void doUpdateMemberInfo(MemberInfo memberInfo, int memberId) throws ValidateException {

        String realName = memberInfo.getStr(MemberInfo.REALNAME);
        String indentify = memberInfo.getStr(MemberInfo.IDENTIFY);
        String email = memberInfo.getStr(MemberInfo.EMAIL);

        Validator validator = new Validator();
        validator.addRule(new StringMustBeRequired(realName,"请输入真实姓名"));
        validator.addRule(new StringLengthBetween(realName,"真实姓名必须是1-20个字符",1,20));
        validator.addRule(new StringMustBeRequired(indentify,"请输入身份证号码"));
        validator.addRule(new StringLengthBetween(indentify,"身份证号码长度不能超过18位",1,18));
        validator.addRule(new StringMustBeEmail(email,"请输入正确的电子邮箱地址"));
        validator.excute();
        MemberInfo temp = getMemberInfo(memberId);
        if(temp==null){
            memberInfo.set(MemberInfo.MEMBER_ID,memberId);
            memberInfo.set(MemberInfo.STATUS,MemberInfo.STATUS_NORMAL);
            memberInfo.set(MemberInfo.CREATE_TIME,new Date());
            memberInfo.save();
        }else{
            memberInfo.set(MemberInfo.MODIFY_TIME,new Date());
            memberInfo.set(MemberInfo.STATUS,MemberInfo.STATUS_NORMAL);
            memberInfo.set(MemberInfo.MEMBER_ID,memberId);
            memberInfo.update();
        }
    }

    public MemberInfo getMemberInfo(int memberId) {
        String sql="select * from "+MemberInfo.dao.tableName()+" where member_id=? limit 1";
        return MemberInfo.dao.findFirst(sql,memberId);
    }


    public void doResetPass(int memberId,String oldPass,String newPass,String confirmPass) throws ValidateException {
        Validator validator = new Validator();
        validator.addRule(new StringMustBePassWord(oldPass,"旧密码必须是字母及数字的组合且长度为6-16位"));
        validator.addRule(new StringMustBePassWord(newPass,"新密码必须是字母及数字的组合且长度为6-16位"));
        validator.addRule(new StringMustBePassWord(confirmPass,"确认密码必须是字母及数字的组合且长度为6-16位"));

        validator.excute();
        Member member = Member.dao.findById(memberId);
        String passFromDb = member.getStr(Member.PASSWORD);
        String salt = member.getStr(Member.SALT);
        oldPass = HashKit.md5(oldPass+salt);
        if(!newPass.equals(confirmPass)){
            throw new ValidateException("新密码与确认密码不一致，请重新输入");
        }
        if(!passFromDb.equals(oldPass)){
            throw new ValidateException("旧密码输入不正确，请重新输入");
        }
        newPass = HashKit.md5(newPass+salt);
        member.set(Member.PASSWORD,newPass);
        member.update();
    }


    public Member doBindMobile(String mobile,int memberId) throws ValidateException {
        Validator validator = new Validator();
        validator.addRule(new StringMustBePassWord(mobile,"绑定的手机号码不能为空"));
        validator.addRule(new StringLengthBetween(mobile,"请输入正确的手机号码格式",1,15));
        validator.excute();
        Member member= MemberService.ME.queryMemberByMobile(mobile);
        if(member!=null){
            throw new ValidateException("当前手机号码已经绑定了系统帐号，无法进行当前操作");
        }
        member = Member.dao.findById(memberId);
        member.set(Member.ID,memberId);
        member.set(Member.MOBILE,mobile);
        member.update();
        return member;
    }
}
